﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kugar.Core.WinForm.ControlFlash
{
    public abstract class FlashItemBase
    {
        protected FlashItemBase(Control sourceControl)
        {
            FlashColor = Color.Red;
            OriginalColor = Color.Transparent;
            CurrentColorType = FlashColorType.OriginalColor;
            Status = FlashType.Dynamic;
            SourceControl = sourceControl;

            var sourceForm = sourceControl.FindForm();

            if(sourceForm!=null)
            {
                sourceForm.FormClosing += new FormClosingEventHandler(sourceForm_FormClosing);
            }
        }

        void sourceForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        public FlashType Status { set; get; }

        public FlashColorType CurrentColorType { set; get; }

        public virtual void Flash()
        {
            var color = this.CurrentColorType == FlashColorType.FlashColor ? this.OriginalColor : this.FlashColor;

            this.Status = FlashType.Dynamic;

            SetColor(color);
        }

        public virtual void Suspend()
        {
            Color color = default(Color);

            switch (Status)
            {
                case FlashType.Static:
                    return;
                case FlashType.Dynamic:
                case FlashType.Disable:
                    color = this.FlashColor;
                    break;
            }

            this.Status = FlashType.Static;

            SetColor(color);
        }

        public virtual void Stop()
        {
            if (this.Status == FlashType.Disable)
            {
                return;
            }

            this.Status = FlashType.Disable;

            var color = this.OriginalColor;

            SetColor(color);
        }

        public virtual Control SourceControl { protected set; get; }

        public Color OriginalColor { set; get; }

        public Color FlashColor { set; get; }

        protected abstract void SetColor(Color color);

        public bool Equals(FlashItemBase other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.SourceControl, SourceControl);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(FlashItemBase)) return false;
            return Equals((FlashItemBase)obj);
        }

        public override int GetHashCode()
        {
            return (SourceControl != null ? SourceControl.GetHashCode() : 0);
        }
    }
}
